package models

import (
	"go-project/common"
	"log"
)

type Roles struct {
	Model
	ID   uint   `gorm:"primary_key" json:"id"`
	Name string `gorm:"type:varchar(255);not null;comment:'名称'" json:"name"`
	Desc string `gorm:"type:varchar(255);comment:'描述'" json:"desc"`
	Key  string `gorm:"type:varchar(255);not null;comment:'key'" json:"key"`
}

func (roles *Roles) TableName() string {
	return "roles"
}

func (roles *Roles) CheckNameExist(name string, id uint) bool {
	query := common.DB.Where("name=?", name)
	if id != 0 {
		query = query.Not("id", id)
	}
	if query.First(roles).RecordNotFound() {
		return false
	}
	return true
}

func (roles *Roles) CheckKeyExist(code string, id uint) bool {
	log.Println("code", code)
	query := common.DB.Where("`key`=?", code)
	if id != 0 {
		query = query.Not("id", id)
	}
	if query.First(roles).RecordNotFound() {
		return false
	}
	return true
}
